package repertory.android.ericchen.androidrepertory.view;

import android.content.Context;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import repertory.android.ericchen.androidrepertory.R;

/**
 * className:BottomPopupDialog
 * author: Ericchen
 * description: 底部弹出框
 * date: 2017/3/8 14:15
 * update: 2017/3/8
 * version:
 */
public class BottomPopupDialog extends PopupDialog{
    private LinearLayout mContainer;

    private View cancelView;

    private TextView mTvTitle;

    private OnDialogChangeListener mListener;

    private Context mContext;

    private Button mOkButton;

    private View mOkDivider;

    private LinearLayout mclose;

    private View mRootView;

    private android.view.View.OnClickListener mOnOkClickListener;

    public BottomPopupDialog(Context context, OnDialogChangeListener listener) {
        super(context);
        mContext = context;
        mListener = listener;
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.comm_widget_dialog);
//		setContentView(mRootView);
        initView();
    }

    private void initView() {
        mContainer = (LinearLayout) findViewById(R.id.center_view);
        cancelView = findViewById(R.id.btn_cancel);
        mTvTitle = (TextView) findViewById(R.id.title);
        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dismissToBottom();
            }
        });
        mOkButton = (Button) findViewById(R.id.btn_ok);
        mOkDivider = findViewById(R.id.kg_btn_ok_divider);
        mclose = (LinearLayout) findViewById(R.id.common_dialog_title_close);
        if (mclose != null){
            mclose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    dismissToBottom();
                }
            });
        }
    }

    public void showFromBottom() {
        mOkDivider.setVisibility(View.GONE);
        super.showFromBottom();
    }

    @Override
    public void show() {
        super.show();
        // mOkDivider.setVisibility(View.VISIBLE);
    }

    public void dismissToBottom() {
        super.dismissToBottom();
    }

    public void removeCenterView(View view) {
        if (view != null) {
            mContainer.removeView(view);
        }
    }

    public void clearCenterView() {
        mContainer.removeAllViews();
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(mContext.getText(titleId));
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTvTitle.setText(title);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        setTitle(String.valueOf(title));
    }

    public void setCancelBtn(String str) {
        ((Button) cancelView).setText(str);
    }

    public void setCancelBtn(Spanned str) {
        ((Button) cancelView).setText(str);
    }

    public void setOKBtn(String str) {
        ((Button) mOkButton).setText(str);
    }

    public void setOKBtn(Spanned str) {
        ((Button) mOkButton).setText(str);
    }

    public void setBtnVisibility(boolean b) {
        if (b) {
            findViewById(R.id.btn_container).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.btn_container).setVisibility(View.GONE);
        }
    }

    public void setTitleAlignType(int alignType) {
        if (alignType == TITLE_ALIGN_TYPE_CENTER) {
            mTvTitle.setGravity(Gravity.CENTER);
        } else if (alignType == TITLE_ALIGN_TYPE_LEFT) {
            mTvTitle.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        } else {
            throw new IllegalArgumentException(
                    "KGBottomDialog align type can only be center or left");
        }
    }

    public TextView getTitleView() {
        return mTvTitle;
    }

    public static final int TITLE_ALIGN_TYPE_CENTER = 1;

    public static final int TITLE_ALIGN_TYPE_LEFT = 2;

    public void addCenterView(View view) {
        if (view != null)
            mContainer.addView(view);

		/*int widthSpec = View.MeasureSpec.makeMeasureSpec(mScreenWidth, View.MeasureSpec.EXACTLY);
		int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		mRootView.measure(widthSpec, heightSpec);

		int measuredH = mRootView.getMeasuredHeight();
		int measuredW = mRootView.getMeasuredWidth();

		KGLog.i("czw_kgbottomwin", "after add ceter view measured w: " + measuredW + " | measured h: " + measuredH);*/
    }

    @Override
    protected void onShow() {
        // TODO Auto-generated method stub
        if (mListener != null)
            mListener.onShow();
    }

    @Override
    protected void onDismiss() {
        // TODO Auto-generated method stub
        if (mListener != null)
            mListener.onDismiss();
    }

    public interface OnDialogChangeListener {
        public void onShow();

        public void onDismiss();
    }

    public void setOnOkButtonClickListener(
            android.view.View.OnClickListener listener) {
        mOkButton.setVisibility(View.VISIBLE);
        mOkDivider.setVisibility(View.VISIBLE);

        if (listener != null) {
            mOnOkClickListener = listener;
            mOkButton.setOnClickListener(mOnOkClickListener);
        }
    }

    public void setOnCancelClickListener(
            android.view.View.OnClickListener listener) {
        if (listener != null && cancelView != null) {
            cancelView.setOnClickListener(listener);
        }
    }

    public void setOnCloseBtnClickListener(
            android.view.View.OnClickListener listener) {
        if (listener != null && mclose != null) {
            mclose.setOnClickListener(listener);
        }
    }

    public void showCloseBtn() {
        mclose.setVisibility(View.VISIBLE);
    }

    public void hideOkBtn() {
        mOkButton.setVisibility(View.GONE);
    }

    public void showOkBtn() {
        mOkButton.setVisibility(View.VISIBLE);
    }

    public void setTitleGravity(int n) {
        ((LinearLayout) findViewById(R.id.title_layout)).setGravity(n);
    }

    public void hideTitleDivider() {
        findViewById(R.id.kg_bottom_dialog_divider).setVisibility(View.GONE);
    }

    public void showButtomDivider() {
        findViewById(R.id.kg_btn_top_divider).setVisibility(View.VISIBLE);
    }

    public void setMainBackgroup(int resId){
        findViewById(R.id.main_bg_layout).setBackgroundResource(resId);
    }

    public void setTitleBackgroup(int resId){
        findViewById(R.id.title_layout).setBackgroundResource(resId);
    }

    public void setBottomBackgroup(int resId){
        findViewById(R.id.title_layout).setBackgroundResource(resId);
    }
}
